import React from 'react';
import { message, Button, Popconfirm, Tooltip } from 'antd';
import classNames from 'classnames';

import BizIcon from '@/components/BizIcon';
import { isSuccess } from '@/utils/request';
import If from '@/components/If';
import authHelper from '@/utils/auth/helper';

import AutoDivider from '@/pages/SalaryManage/components/AutoDivider';
import ExcelListBaseModel from '@/pages/SalaryManage/SalaryCalculate/ImportExcel/ExcelListBaseModel';
import {
    queryTaxInitByPage,
    salaryPayrollTaxDownload,
    deleteSalaryPayrollTax
} from '@/services/salaryManage/salaryCalculate';
import { amountColumn } from '@/pages/SalaryManage/utils/TableUtil';
import salaryStyle from '@/pages/SalaryManage/salaryManage.less';
import { goToSalaryPayrollTaxLog } from '@/pages/SalaryManage/utils/pageNavigateUtil';

const namespace = 'salaryPayrollTaxModel';
const SalaryPayrollTaxModel = ExcelListBaseModel({
    namespace,
    queryService: queryTaxInitByPage,
    initService: null
});

SalaryPayrollTaxModel.state.goToLog = goToSalaryPayrollTaxLog;
SalaryPayrollTaxModel.state.columns = (pagination, { onEdit, onDelete }) => [
    {
        title: '序号',
        dataIndex: 'sn',
        render(text, record, index) {
            return !record.$$isSummary ? `${(pagination.current - 1) * pagination.pageSize + index + 1}` : '';
        },
        align: 'center',
        width: 80,
        fixed: 'left'
    },
    {
        title: '姓名',
        dataIndex: 'name',
        width: 132,
        fixed: 'left',
        render(text, record /* , index */) {
            return !record.$$isSummary ? text : '合计';
        }
    },
    {
        title: '工号',
        dataIndex: 'employeeNo',
        width: 150
    },
    {
        title: '截止年月',
        dataIndex: 'dataPeriod',
        width: 100,
        render(text, record) {
            if (record.year && record.month) {
                return `${record.year}-${record.month < 10 ? '0' : ''}${record.month}`;
            }

            return '';
        }
    },
    amountColumn({
        title: (
            <div className={salaryStyle.textCenter}>
                <div>累计税前收入</div>
                <div>（截止当月）</div>
            </div>
        ),
        dataIndex: 'encryptTotalIncomeAmt'
    }),
    amountColumn({
        title: (
            <div className={salaryStyle.textCenter}>
                <div>累计免税扣除额</div>
                <div>（截止当月）</div>
            </div>
        ),
        dataIndex: 'encryptTotalNotaxAmt',
        width: 130
    }),
    amountColumn({
        title: (
            <div className={salaryStyle.textCenter}>
                <div>累计计税基数</div>
                <div>（截止当月）</div>
            </div>
        ),
        dataIndex: 'encryptTotalTaxIncomeAmt'
    }),
    amountColumn({
        title: (
            <div className={salaryStyle.textCenter}>
                <div>累计已缴个税</div>
                <div>（截止当月）</div>
            </div>
        ),
        dataIndex: 'encryptTotalPayedTaxAmt'
    }),

    {
        title: '累计险金（截止当月）',
        dataIndex: 'ljwxyj',
        align: 'center',
        children: [
            amountColumn(
                {
                    title: '养老保险（个人）',
                    dataIndex: 'encryptTotalFundOldAmt',
                    width: 150
                },
                { color: true }
            ),
            amountColumn(
                {
                    title: '失业保险（个人）',
                    dataIndex: 'encryptTotalFundUnemployAmt',
                    width: 150
                },
                { color: true }
            ),
            amountColumn(
                {
                    title: '医疗保险（个人）',
                    dataIndex: 'encryptTotalFundHospitalAmt',
                    width: 150
                },
                { color: true }
            ),
            amountColumn(
                {
                    title: '生育保险（个人）',
                    dataIndex: 'encryptTotalFundBirthBaseAmt',
                    width: 150
                },
                { color: true }
            ),
            amountColumn(
                {
                    title: '工伤保险（个人）',
                    dataIndex: 'encryptTotalFundHarmBaseAmt',
                    width: 150
                },
                { color: true }
            ),
            amountColumn(
                {
                    title: '大病保险（个人）',
                    dataIndex: 'encryptTotalFundIllBaseAmt',
                    width: 150
                },
                { color: true }
            ),
            amountColumn(
                {
                    title: '公积金（个人）',
                    dataIndex: 'encryptTotalFundHouseAmt',
                    width: 150
                },
                { color: true }
            )
        ]
    },
    {
        title: '专项扣款（截止当月）',
        dataIndex: 'zxkk',
        align: 'center',
        children: [
            amountColumn({
                title: '子女教育',
                dataIndex: 'encryptCurrChildEducationAmt'
            }),
            amountColumn({
                title: '继续教育',
                dataIndex: 'encryptCurrContinueEducationAmt'
            }),
            amountColumn({
                title: '住房贷款利息',
                dataIndex: 'encryptCurrHouseLoanInterestAmt'
            }),
            amountColumn({
                title: '住房租金',
                dataIndex: 'encryptCurrHouseLeaseAmt'
            }),
            amountColumn({
                title: '赡养老人',
                dataIndex: 'encryptCurrRaiseOldAmt'
            })
        ]
    },
    amountColumn({
        title: (
            <div className={salaryStyle.textCenter}>
                <div>累计免征额</div>
                <div>（截止当月）</div>
            </div>
        ),
        dataIndex: 'encryptTotalExpenseAmt',
        width: 140
    }),
    amountColumn({
        title: (
            <div className={salaryStyle.textCenter}>
                <div>累计税后加减项</div>
                <div>（截止当月）</div>
            </div>
        ),
        dataIndex: 'encryptTotalAfterTaxAmt',
        width: 140
    }),
    {
        title: '操作',
        dataIndex: 'v_operate',
        fixed: 'right',
        width: 100,
        render(text, record) {
            return (
                <>
                    {
                        !record.$$isSummary
                            ? <AutoDivider>
                                <If condition={authHelper.checkCode('finance:salary:computation:import:taxinit:edit')}>
                                    <Tooltip title="编辑">
                                        <Button
                                            className={classNames(salaryStyle.operateIcon)}
                                            onClick={() => onEdit(record)}
                                            disabled={!!record?.payrollIfProcessed}
                                        >
                                            <BizIcon type="listfunction_edit_icon_default" />
                                        </Button>
                                    </Tooltip>
                                </If>
                                <If condition={authHelper.checkCode('finance:salary:computation:import:taxinit:del')}>
                                    <Popconfirm
                                        title={`是否确定删除【${record.name}】的个税初始化信息`}
                                        placement="bottomRight"
                                        onConfirm={() => onDelete(record)}
                                        disabled={!!record?.payrollIfProcessed}
                                    >
                                        <Tooltip title="删除" placement="top">
                                            <Button
                                                className={classNames(salaryStyle.operateIcon)}
                                                disabled={!!record?.payrollIfProcessed}
                                            >
                                                <BizIcon type="DeleteIcon" />
                                            </Button>
                                        </Tooltip>
                                    </Popconfirm>
                                </If>
                            </AutoDivider>
                            : null
                    }

                </>
            );
        }
    }
];

SalaryPayrollTaxModel.effects.init = function* init(state, { put, select }) {
    if (state?.payload?.needReset) {
        yield put({
            type: 'setMultiStore',
            payload: {
                searchModel: {
                    employeeCode: null,
                    period: null
                }
            }
        });
    }

    if (state?.payload?.organizationCode) {
        const searchModel = yield select(s => s[namespace].searchModel);
        yield put({
            type: 'setMultiStore',
            payload: {
                searchModel: {
                    ...searchModel,
                    organizationCode: state?.payload?.organizationCode
                }
            }
        });
    }
    // 查询
    yield put({ type: 'search' });
};

SalaryPayrollTaxModel.effects.downloadTemplate = function* downloadExcel({ payload }, { call }) {
    return yield call(salaryPayrollTaxDownload, payload);
};

SalaryPayrollTaxModel.effects.deleteSalaryPayrollTax = function* deleteTax(
    { payload, callback, currentRecord },
    { call, put }
) {
    yield put({
        type: 'setMultiStore',
        payload: { pageLoading: true }
    });
    const response = yield call(deleteSalaryPayrollTax, { id: payload });
    yield put({
        type: 'setMultiStore',
        payload: { pageLoading: false }
    });

    if (isSuccess(response)) {
        message.success(`【${currentRecord.name}】的个税初始化删除成功`);
        yield put({ type: 'search' });
    } else {
        message.error(response.msg);
    }

    if (callback) {
        callback(response);
    }
};

export default SalaryPayrollTaxModel;
